package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 设置编码
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");

            // 获取请求的调用方法名称
            String methodName = request.getParameter("methodName");

            // 反射
            // 获取当前类的字节码对象
            Class clazz = this.getClass();

            // 获取方法对象
            Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);

            // 执行方法
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 删除
    public void deleteStu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1. 获取删除学号
        String number = request.getParameter("number");
        //2. 调用service删除
        StudentService service = new StudentServiceImpl();
        service.deleteStu(number);
    }

    // 修改
    public  void updateStu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            // 获取请求参数
            Map<String, String[]> map = request.getParameterMap();

            // 封装Student
            Student student = new Student();

            // 注册日期转换器方法
            dateConvert();
            BeanUtils.populate(student,map);

            // 调用service方法
            StudentService service = new StudentServiceImpl();
            service.updateStu(student);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 添加
    public  void addStu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try{
            // 获取请求参数
            Map<String, String[]> map = request.getParameterMap();

            // 封装Student
            Student student = new Student();

            // 注册日期转换器方法
            dateConvert();
            BeanUtils.populate(student,map);

            // 调用service方法
            StudentService service = new StudentServiceImpl();
            service.addStu(student);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 分页查询
    public  void findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
       // 获取分页参数
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");

        // 调用service查询
        StudentService service = new StudentServiceImpl();
        PageInfo info = service.findByPage(currentPage,pageSize);

        // 将对象转换为JSON，返回到浏览器端
        new ObjectMapper().writeValue(response.getWriter(),info);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    // 日期转换
    private void dateConvert() {
        ConvertUtils.register(new Converter() {
            public Object convert(Class type, Object value) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return simpleDateFormat.parse(value.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
    }
}
